﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using ARR.UI;
using ARR.UI.ViewModels;
using System.ServiceModel;
using ARR.Entities;
using ARR.UI.Views;
using ARR.Shared;
using System.Windows.Controls;
using ARR.UI.CountryService;
using System.Text;

namespace ARR
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application 
    {
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;

            DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);

            InitializeComponent();
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if(e.Exception is FaultException<ARRExceptionDetail>)
            {
                var ex = e.Exception as FaultException<ARRExceptionDetail>;
                
                (new Alert(ex.Detail.Message, "No se pudó realizar la operación", AlertType.Warning)).ShowDialog();

                e.Handled = true;
            }
            else if (e.Exception is ARRException)
            {
                var ex = e.Exception as ARRException;

                (new Alert(ex.Message, "No se pudó realizar la operación", AlertType.Warning)).ShowDialog();

                e.Handled = true;
            }
            else
            {
                ShowUnhandledExceptionMessage(e.Exception);
                e.Handled = true;

                //DB: Aca deberia ir el codigo que cierre la aplicación y libere todos los recursos en uso.
                // por ahora queda asi para que se vea claramente donde fue que explotó.
            }

        }

        private void ShowUnhandledExceptionMessage(Exception exception)
        {
            Exception ex = GetOriginalException(exception);


            StringBuilder sb = new StringBuilder();
            sb.AppendLine(Errors.UnexpectedException);
            sb.AppendLine();
            sb.AppendLine(ex.Message);
            sb.AppendLine();
            sb.AppendLine(ex.StackTrace);

            new Alert(sb.ToString(), "Error inesperado", AlertType.Error).ShowDialog();         
            //TODO: Loggear la excepción.
        }
                
        private void End()
        {
            if (Application.Current != null)
            {
                Application.Current.Shutdown();
            }
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }

        private Exception GetOriginalException(Exception ex)
        {
            if (ex.InnerException != null)
            {
                return GetOriginalException(ex.InnerException);
            }
            else
            {
                return ex;
            }
        }
    }
}
